<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考勤日期">
        <el-date-picker
          v-model="daterangeAccountDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
  <el-form-item label="员工" prop="partyId">
    <el-select v-model="queryParams.partyId" placeholder="请选择员工" clearable filterable>
      <el-option
          v-for="dict in partyIdOptions"
          :key="dict.partyId"
          :label="dict.firstName"
          :value="dict.partyId"
      />
    </el-select>
  </el-form-item>
  <el-form-item label="考勤状况" prop="attendType">
    <el-select v-model="queryParams.attendType" placeholder="请选择考勤状况" clearable>
      <el-option
          v-for="dict in attendTypeOptions"
          :key="dict.enumCode"
          :label="dict.description"
          :value="dict.enumCode"
      />
    </el-select>
  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleDetail"
          v-hasPermi="['ehr:attend_daily_account:edit']"
        >详情</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="repeatSign"
          v-hasPermi="['ehr:attend_daily_account:edit']"
        >补签</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ehr:attend_daily_account:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehr:attend_daily_account:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehr:attend_daily_account:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehr:attend_daily_account:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attend_daily_accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考勤日期" align="center" prop="accountDate" width="105">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accountDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工" align="center" prop="partyId"  :formatter="partyIdFormat"/>
  <el-table-column label="上班时间" align="center" prop="onWrokStartTime" width="170">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.onWrokStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column>
  <el-table-column label="下班时间" align="center" prop="offWrokStartTime" width="170">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.offWrokStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column>
      <el-table-column label="上班工时" align="center" prop="workTimeTotal" />
      <el-table-column label="迟到分钟" align="center" prop="lateMin" />
      <el-table-column label="早退分钟" align="center" prop="earlyMin" />
      <el-table-column label="考勤状况" align="center" prop="attendType" :formatter="attendTypeFormat"/>
  <el-table-column label="更新时间" align="center" prop="lastUpdatedStamp" width="170">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:attend_daily_account:detail']"
          >详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="goOut(scope.row)"
          >外出申请</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="askLeave(scope.row)"
          >请假</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="repeatSign(scope.row)"
          >补签</el-button>
          
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:attend_daily_account:edit']"
          >修改</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:attend_daily_account:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考勤日报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled = detailFlag>
        <el-row>
          <el-col :span="12" >
                <el-form-item label="考勤日期" prop="accountDate">
                    <el-date-picker clearable
                        v-model="form.accountDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择考勤日期">
                    </el-date-picker>
                </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="员工" prop="partyId">
              <el-select v-model="form.partyId" placeholder="请选择员工">
                <el-option
                    v-for="dict in partyIdOptions"
                    :key="dict.partyId"
                    :label="dict.firstName"
                    :value="dict.partyId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
              <el-form-item label="上班时间" prop="onWrokStartTime">
                <el-date-picker clearable
                    v-model="form.onWrokStartTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    default-time = "00:00:00"
                    placeholder="请选择上班开始时间">
                </el-date-picker>
              </el-form-item>
          </el-col>
          <el-col :span="12" >
              <el-form-item label="下班时间" prop="offWrokStartTime">
                <el-date-picker clearable
                    v-model="form.offWrokStartTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    default-time = "00:00:00"
                    placeholder="请选择下班开始时间">
                </el-date-picker>
              </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
            <el-form-item label="考勤规则" prop="attendRuleId">
                <el-input v-model="form.attendRuleId" placeholder="请输入考勤规则" />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="上班工时" prop="workTimeTotal">
                <el-input v-model="form.workTimeTotal" placeholder="请输入上班工时" />
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
            <el-form-item label="迟到分钟" prop="lateMin">
                <el-input v-model="form.lateMin" placeholder="请输入迟到分钟" />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="早退分钟" prop="earlyMin">
                <el-input v-model="form.earlyMin" placeholder="请输入早退分钟" />
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="20" >
            <el-form-item label="考勤状况" prop="attendType">
              <el-select v-model="form.attendType" placeholder="请选择考勤状况">
                <el-option
                    v-for="dict in attendTypeOptions"
                    :key="dict.enumCode"
                    :label="dict.description"
                    :value="dict.enumCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row >
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { 
  listAttend_daily_account, 
  getAttend_daily_account, 
  delAttend_daily_account, 
  addAttend_daily_account, 
  updateAttend_daily_account } from "@/api/ehr/attend_abnormal";

export default {
  name: "Attend_daily_account",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤日报表格数据
      attend_daily_accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 考勤状况时间范围
      daterangeAccountDate: [],
      // 员工业务字典
      partyIdOptions: [],
      // 考勤状况业务字典
      attendTypeOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc:"", // 升序或降序 大写的 ASC或DESC,为空代表升序
        accountDate: null,
        partyId: null,
        attendType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
  // 员工业务字典
  this.getEntityDicts("person:partyId:firstName").then((response) => {
    this.partyIdOptions = response.data;
  });
    // 考勤状况业务字典
    this.getEnumDicts("Attend_Daily_Error").then((response) => {
      this.attendTypeOptions = response.data;
    });
    this.getList();
  },
  methods: {
     partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
     attendTypeFormat(row, column) {
      let val = null;
      this.attendTypeOptions.forEach((item, index) => {
        if (row.attendType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询考勤日报列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAccountDate && '' != this.daterangeAccountDate) {
        this.queryParams.params["beginAccountDate"] = this.daterangeAccountDate[0];
        this.queryParams.params["endAccountDate"] = this.daterangeAccountDate[1];
      }
      listAttend_daily_account(this.queryParams).then(response => {
        this.attend_daily_accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dailyAcountId: null,
        accountDate: null,
        partyId: null,
        onWrokStartTime: null,
        onWrokEndTime: null,
        offWrokStartTime: null,
        offWrokEndTime: null,
        attendRuleId: null,
        workTimeTotal: null,
        standardWorkHours: null,
        lateMin: null,
        earlyMin: null,
        attendType: "0",
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAccountDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dailyAcountId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
      this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加考勤日报";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
      this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const dailyAcountId = row.dailyAcountId || this.ids
      getAttend_daily_account(dailyAcountId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "考勤日报详情";
      });
    },
    /** 外出申请按钮操作 */
    goOut(row) {
      // todo
    },
    /** 请假按钮操作 */
    askLeave(row) {
      // todo
    },
    /** 补签按钮操作 */
    repeatSign(row) {
      // todo
      const dailyAcountId = row.dailyAcountId || this.ids
      console.info("dailyAcountId:"+dailyAcountId);
      var _path = "/workflow/approve/sign_agin/apply";
      this.$router.push({
        path: _path,
        query: {
          // 异常考勤记录ID
          dailyAcountId: dailyAcountId,
        },
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const dailyAcountId = row.dailyAcountId || this.ids
      getAttend_daily_account(dailyAcountId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤日报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dailyAcountId != null && this.updateFlag) {
            updateAttend_daily_account(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttend_daily_account(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dailyAcountIds = row.dailyAcountId || this.ids;
      this.$modal.confirm('是否确认删除考勤日报编号为"' + dailyAcountIds + '"的数据项？').then(function() {
        return delAttend_daily_account(dailyAcountIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/attend_daily_account/abnormalexport', {
        ...this.queryParams
      }, `attend_daily_account_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
